package kr.ac.hanyang.cs.park;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;


public class PkgInfo extends OptionInfo {
	private String name										= "";
	private String option									= "";		// need PACKAGE NAME to launch external app
	private String classname								= "";
	private int versionCode									= 0;
	private boolean includeSystemPackages					= false;	// false: do not include system packages
	private boolean isDebug									= false;
	private Activity objActivity							= null;		// should be given for getPackageManager() method
	private Drawable icon									= null;

	
	public PkgInfo() {
		super();
	}

	public PkgInfo(Activity objActivity) {
		this();
		this.objActivity									= objActivity;
	}


	public void setDebug(boolean isDebug) {
		this.isDebug										= isDebug;
	}


	private void prettyPrint() {
		Log.d("Applist ",name + "   " + option + "   " + classname + "   " + versionCode);
	}


	public ArrayList<OptionInfo> getPacakageInfo() {
		ArrayList<PkgInfo> appList							= getInstalledApps(includeSystemPackages);
		ArrayList<OptionInfo> returnList			= new ArrayList<OptionInfo>();
		
		for (int i = 0; i < appList.size();i++) {
			OptionInfo aItem						= new OptionInfo();

			aItem.setName(appList.get(i).getName());
			aItem.setOption(appList.get(i).getOption());
			aItem.setIcon(appList.get(i).getIcon());
			
			returnList.add(aItem);
		}
		
		return returnList;
	}


	public ArrayList<PkgInfo> getPackageInfo() {
		ArrayList<PkgInfo> appList							= getInstalledApps(includeSystemPackages);

		final int appCount									= appList.size();
		if (isDebug) {
			Log.d("AppNo", "Installed Packages:"); 
			Log.d("AppNo", "No of apps "+ appCount);
			for (int i = 0;i < appCount;i++)
				appList.get(i).prettyPrint();
		}

		return appList;
	}

    
	public ArrayList<PkgInfo> getInstalledApps(boolean getSysPackages) {
		ArrayList<PkgInfo> appList							= new ArrayList<PkgInfo>();
		List<PackageInfo> packs								= objActivity.getPackageManager().getInstalledPackages(0);

		for(int i = 0;i < packs.size();i++) {
			PackageInfo p									= packs.get(i);

			if ((!getSysPackages) && (p.versionName == null))
				continue;

			if (classname == null || classname.equals("null"))
				continue;

			PkgInfo newInfo									= new PkgInfo();
	        newInfo.name									= p.applicationInfo.loadLabel(objActivity.getPackageManager()).toString();
	        newInfo.option									= p.packageName;
	        newInfo.classname								= p.applicationInfo.className;
	        newInfo.versionCode								= p.versionCode;
	        newInfo.icon									= p.applicationInfo.loadIcon(objActivity.getPackageManager());

	        Intent app										= objActivity.getPackageManager().getLaunchIntentForPackage(p.packageName);

	        if(app != null){
	        	//dlteIntent(app,newInfo.name, newInfo.icon);
	        	appList.add(newInfo);
	        }
	    }

	    return appList;
	}

	public String getName() {
		return name;
	}

	public String getOption() {
		return option;
	}

	public String getClassname() {
		return classname;
	}

	public int getVersionCode() {
		return versionCode;
	}

	public Drawable getIcon() {
		return icon;
	}

}
